package com.sambo.inspection.component;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sambo.inspection.R;
import com.sambo.inspection.adapter.CommonAdapter;
import com.sambo.inspection.adapter.ViewHolder;
import com.sambo.inspection.bean.PartitionBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import razerdp.basepopup.BasePopupWindow;

public class SlideFromTopPopup extends BasePopupWindow {

      private PartitionSelectListener mListener;

      private ListView lvTreeFirst;
      private ListView lvTreeSecond;
      private ListView lvTreeThird;
      private TextView tvReset;
      private TextView tvConfirm;

      private TreeListAdapter mTreeFirstAdapter;
      private TreeListAdapter mTreeSecondAdapter;
      private TreeListAdapter mTreeThirdAdapter;

      private List<PartitionBean> mTreeFirstDataList;
      private List<PartitionBean> mTreeSecondDataList;
      private List<PartitionBean> mTreeThirdDataList;

      private List<PartitionBean> mTreeDataList;
      private Map<String, PartitionBean> mTreeDataMap;
      private String mRootPartitionID;

      public SlideFromTopPopup(Context context) {
            super(context);
            setBackPressEnable(true);
            setDismissWhenTouchOutside(true);

            lvTreeFirst = (ListView) findViewById(R.id.lv_tree_first);
            lvTreeSecond = (ListView) findViewById(R.id.lv_tree_second);
            lvTreeThird = (ListView) findViewById(R.id.lv_tree_third);
            tvReset = (TextView) findViewById(R.id.tv_reset);
            tvConfirm = (TextView) findViewById(R.id.tv_confirm);

            mTreeFirstDataList = new ArrayList<>();
            mTreeSecondDataList = new ArrayList<>();
            mTreeThirdDataList = new ArrayList<>();

            mTreeDataList = new ArrayList<>();
            mTreeDataMap = new HashMap<>();

            mTreeFirstAdapter = new TreeListAdapter(context, mTreeFirstDataList);
            mTreeSecondAdapter = new TreeListAdapter(context, mTreeSecondDataList);
            mTreeThirdAdapter = new TreeListAdapter(context, mTreeThirdDataList);

            lvTreeFirst.setAdapter(mTreeFirstAdapter);
            lvTreeSecond.setAdapter(mTreeSecondAdapter);
            lvTreeThird.setAdapter(mTreeThirdAdapter);

            lvTreeFirst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                      mTreeFirstAdapter.setCurrentPosition(i);
                      mTreeSecondAdapter.setCurrentPosition(0);
                      mTreeThirdAdapter.setCurrentPosition(0);
                      selectTreeFirstData(mTreeFirstDataList.get(i));
                  }
            });

            lvTreeSecond.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                      mTreeSecondAdapter.setCurrentPosition(i);
                      mTreeThirdAdapter.setCurrentPosition(0);
                      selectTreeSecondData(mTreeSecondDataList.get(i));
                  }
            });

            lvTreeThird.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                  }
            });

            tvReset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mTreeSecondDataList.clear();
                    mTreeSecondAdapter.notifyDataSetChanged();
                    mTreeThirdDataList.clear();
                    mTreeThirdAdapter.notifyDataSetChanged();

                    if (mTreeDataList != null && mTreeDataList.size() > 0) {
                        for (PartitionBean bean : mTreeDataList) {
                            bean.setSelected(false);
                        }
                    }

                    if (mListener != null) {
                        mListener.onSelectPartition(null);
                    }
                    dismiss();
                }
            });

            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
      }

      public void setTreeData(List<PartitionBean> dataList) {

          mTreeDataList.clear();
          mTreeFirstDataList.clear();
          mTreeSecondDataList.clear();
          mTreeThirdDataList.clear();

          mTreeDataList.addAll(dataList);

          if (mTreeDataList != null && mTreeDataList.size() > 0) {
              for (PartitionBean bean : mTreeDataList) {
                  mTreeDataMap.put(bean.getId(), bean);
                  if (TextUtils.equals(bean.getPid(), "0")) {
                      mRootPartitionID = bean.getId();
                  }
              }

              // 筛选出一级菜单项
              for (PartitionBean bean : mTreeDataList) {
                  if (TextUtils.equals(mRootPartitionID, bean.getPid())) {
                      mTreeFirstDataList.add(bean);
                  }
              }
          }

          mTreeFirstAdapter.setCurrentPosition(-1);
          mTreeSecondAdapter.setCurrentPosition(-1);
          mTreeThirdAdapter.setCurrentPosition(-1);

      }

      public void setPartitionSelectListener(PartitionSelectListener mListener) {
          this.mListener = mListener;
      }

      @Override
      protected Animation initShowAnimation() {
            TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, -dipToPx(350f), 0);
            translateAnimation.setDuration(450);
            translateAnimation.setInterpolator(new OvershootInterpolator(-1));
            return translateAnimation;
      }

      @Override
      protected Animation initExitAnimation() {
            TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, 0, -dipToPx(350f));
            translateAnimation.setDuration(200);
            translateAnimation.setInterpolator(new OvershootInterpolator(-1));
            return translateAnimation;
      }

      @Override
      public View getClickToDismissView() {
            return getPopupWindowView();
      }

      @Override
      public View onCreatePopupView() {
            return createPopupById(R.layout.popup_slide_menu);
      }

      @Override
      public View initAnimaView() {
            return findViewById(R.id.popup_anim);
      }

      private void selectTreeFirstData(PartitionBean item) {

          if (TextUtils.equals(item.getISLEAF(), "1")) {
              mTreeSecondDataList.clear();
              mTreeSecondAdapter.notifyDataSetChanged();

              if (mListener != null) {
                  mListener.onSelectPartition(item);
              }

          } else {
              mTreeSecondDataList.clear();

              // 筛选出一级菜单项
              for (PartitionBean bean : mTreeDataList) {
                  if (TextUtils.equals(item.getId(), bean.getPid())) {
                      mTreeSecondDataList.add(bean);
                  }
              }

              if (mTreeSecondDataList.size() > 0) {
                  mTreeSecondAdapter.setCurrentPosition(0);
                  if (mListener != null) {
                      mListener.onSelectPartition(mTreeSecondDataList.get(0));
                  }
                  selectTreeSecondData(mTreeSecondDataList.get(0));
              }

              mTreeSecondAdapter.notifyDataSetChanged();
          }
      }

    private void selectTreeSecondData(PartitionBean item) {

        if (TextUtils.equals(item.getISLEAF(), "1")) {
            mTreeThirdDataList.clear();
            mTreeThirdAdapter.notifyDataSetChanged();

            if (mListener != null) {
                mListener.onSelectPartition(item);
            }

        } else {
            mTreeThirdDataList.clear();

            // 筛选出一级菜单项
            for (PartitionBean bean : mTreeDataList) {
                if (TextUtils.equals(item.getId(), bean.getPid())) {
                    mTreeThirdDataList.add(bean);
                }
            }

            if (mTreeThirdDataList.size() > 0) {
                mTreeThirdAdapter.setCurrentPosition(0);
                if (mListener != null) {
                    mListener.onSelectPartition(mTreeThirdDataList.get(0));
                }
            }

            mTreeThirdAdapter.notifyDataSetChanged();
        }
    }

    class TreeListAdapter extends CommonAdapter<PartitionBean> {

        List<PartitionBean> mDataList;

        public TreeListAdapter(Context context, List<PartitionBean> mDatas) {
            super(context, mDatas, R.layout.item_tree_partition);
            mDataList = mDatas;
        }

        @Override
        public void convert(final ViewHolder helper, PartitionBean item) {

            helper.setText(R.id.tv_partition_name, item.getDeptName());

            if (item.isSelected()) {
                helper.setTextColor(R.id.tv_partition_name, getContext().getResources().getColor(R.color.weixin_green));
            } else {
                helper.setTextColor(R.id.tv_partition_name, getContext().getResources().getColor(R.color.font_normal));
            }
        }

        public void setCurrentPosition(int position) {
            for (PartitionBean bean : mDataList) {
                bean.setSelected(false);
            }

            if (position > -1 && position < mDataList.size()) {
                mDataList.get(position).setSelected(true);
            }

            notifyDataSetChanged();
        }
    }

    public interface PartitionSelectListener {
      void onSelectPartition(PartitionBean bean);
    }
}